//
//  YWReadViewController.m
//  网易新闻
//
//  Created by Yeewii@hotmail.com on 16/6/15.
//  Copyright © 2016年 Yeewii@hotmail.com. All rights reserved.
//

#import "YWReadViewController.h"
#import "YWHttpTool.h"
#import "YWReadNSObject.h"
#import "YWReadInternalBaseClass1.h"
#import "YWReadCell.h"
#import "UITableView+SDAutoTableViewCellHeight.h"
#import "YWReadPicCell.h"
#import "YWUnLikeView.h"
#import <MBProgressHUD+BWMExtension.h>


static NSString *const CellReuseIdentifier = @"CellReuseIdentifier_YWReadCell";
static NSString *const PicCellReuseIdentifier = @"PicCellReuseIdentifier";

@interface YWReadViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@end

@implementation YWReadViewController
{
    NSMutableArray *_dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestData];
}

-(UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView            = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, MAINWIDTH, MAINHEIGHT)];
        _tableView.delegate   = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
        [_tableView registerClass:[YWReadCell class] forCellReuseIdentifier:CellReuseIdentifier];
        [_tableView registerNib:[UINib nibWithNibName:@"YWReadPicCell" bundle:nil] forCellReuseIdentifier:PicCellReuseIdentifier];
    }
    return _tableView;
}

-(void)requestData
{
    [YWHttpTool GET:READURL params:nil success:^(NSDictionary *responseObject) {
        
        YWReadNSObject *readModel = [YWReadNSObject modelObjectWithDictionary:responseObject];
        _dataArray = [NSMutableArray arrayWithArray:readModel.myProperty1];
        
        
        for (YWReadInternalBaseClass1 *dataModel in _dataArray)
        {
//            NSLog(@"%@",dataModel);
        }
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];

}


#pragma mark - delegate    and     dataSource -
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YWReadInternalBaseClass1 *model = _dataArray[indexPath.row];
    return model.imgnewextra.count>0?[YWReadPicCell fixCellHeight]:[self cellHeightForIndexPath:indexPath cellContentViewWidth:MAINWIDTH tableView:tableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    __weak typeof(self) weakSelf = self;
    YWReadInternalBaseClass1 *model = _dataArray[indexPath.row];
    if (model.imgnewextra.count>0)
    {
        YWReadPicCell *cell = [tableView dequeueReusableCellWithIdentifier:PicCellReuseIdentifier];
        cell.model = model;
        return cell;
    }else
    {
        YWReadCell *cell                = [tableView dequeueReusableCellWithIdentifier:CellReuseIdentifier];
        cell.model                      = model;
        cell.deleteBlock = ^(NSArray *unLike){
        
            
            YWUnLikeView *unLikeView = [[YWUnLikeView alloc]initWithUnLikeArray:unLike];
            [weakSelf.view.window addSubview:unLikeView];
            unLikeView.selectBlock = ^(NSString *selectString){
            
                
                [MBProgressHUD bwm_showTitle:[NSString stringWithFormat:@"举报原因:%@",selectString] toView:weakSelf.view hideAfter:3];
                [_dataArray removeObjectAtIndex:indexPath.row];
                [weakSelf.tableView reloadData];
            };
            
        };
        return cell;
    }
    
    
}
@end
